import React, { Component } from 'react';
import config from '../../pages/list/config';
// import requestApi from '../../../../expenseaccount/wallet/card/requestApi';
import { onSearchButtonClick } from '../../pages/list/events';

import './index.less';
class Table extends Component {
    constructor(props) {
        super(props);
        this.state = {
        }
    }

    // 组件挂载完成时触发的函数~~~只执行一次
    componentDidMount() {
        if(this.props && this.props.onTableAreaRef){
            this.props.onTableAreaRef(this)
        }  
    }
    
    render() {
        let { createTransferTable, that} = this.props;
        // const multiLang = this.props.MutiInit.getIntl(2011); 
        return (
            <div className="nc-singleTable-table-area nc-singleTable-table-area-flex">
                <div className = "area-content-main order-content">
                {createTransferTable(
                    {
                        headTableId: config.orderConfig.orderListArea,//表格组件id
                        bodyTableId: config.orderConfig.bodysArea,//子表模板id
                        fullTableId: config.orderConfig.bodysArea,//主子拉平模板id
                        showGenerateBtn: false, // 隐藏右下角按钮
                        comType: that.state.navTabStatus === '33' ? 'hasCheckBox_child' : '',
                        showSeletedBtn: that.state.navTabStatus === '33',
                        //点击加号展开，设置表格数据
                        containerSelector: '#transferInvoiceWallet', //容器的选择器 必须唯一,用于设置底部已选区域宽度
                        cacheItem:[],
                        showPagination:true,
                        onPageInfoChange: onSearchButtonClick.bind(that),
                        onCheckedChange: (flag, record, index, bodys) => {                   
                            // //全选
                            if(Array.isArray(record)) {
                                const keys =  record.map(itm => itm.pk_ermorder.value)
                                keys.forEach(key => {
                                    if (flag == true) {
                                        that.orderDeleteList[key] = true
                                    } else if (flag == false) {
                                        that.orderDeleteList[key] = false
                                        that.setState({orderCheckedList: []})
                                    }
                                })
                            } else {
                                //单选条件
                                const key = record.pk_ermorder.value
                                let child_key = record.pk_ermorder_b && record.pk_ermorder_b.value
                                //勾选子表 -> 全选
                                let body_keys = []
                                that.orderList.forEach(item => {
                                    if(item.head.head.rows[0].values.pk_ermorder.value === key) {
                                        if(item.body) {
                                            body_keys = item.body.body.rows.map(_itm => {  
                                                if(_itm.values.pk_ermorder_b.value !== child_key) return _itm.values.pk_ermorder_b.value }
                                            )
                                        }
                                    }
                                })
                                //只勾选兄弟元素，否则计算错误
                                body_keys = body_keys.filter(itm => itm && itm)
                                if(child_key && body_keys.length > 0 && flag) {
                                    that.props.transferTable.setTheCheckRows('head', body_keys)
                                }
                                if(child_key && body_keys.length > 0 && (!flag)) {
                                    that.props.transferTable.cancelRowsChecked('head', [key])
                                }

                                if (flag == true) {
                                    that.orderDeleteList[key] = true
                                } else if (flag == false) {
                                    that.orderDeleteList[key] = false
                                    // that.checkedList = []
                                    that.setState({orderCheckedList: []})
                                }
                                console.log(that.orderDeleteList)
                                if(flag === true && (that.orderDeleteList && Object.keys(that.orderDeleteList).length > 0)) {
                                    let checkedListNew = that.state.orderCheckedList;
                                    checkedListNew.push({
                                        isOptimized: false,
                                        values: record
                                    })

                                    that.setState({orderCheckedList: checkedListNew})
                                }
                                console.log(that.state.orderCheckedList,'orderCheckedList======')
                            }
                        },
                    })}
                </div>
            </div>
        )
    }
}
export default Table;
